﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMP.ERP.BLL;
using CMP.ERP.Model;
using System.Data;

namespace CMP.ERP.Admin.Controllers
{
    public class ProcurementController : Controller
    {
        //
        // GET: /Procurement/
        //实例化对象
        SupplierBLL pbll = new SupplierBLL();
        PorBuyBLL bll = new PorBuyBLL();
        WareHoseBLL dbll = new WareHoseBLL();

        /// <summary>
        /// 显示采购订单数据信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Procurement_Show()
        {
            NumberClassBLL cbll = new NumberClassBLL();
            Session["num"] = cbll.GetRandom2();
            List<Supplier> list = new List<Supplier>();
            DataTable data = pbll.SupplierAll();
            foreach (DataRow item in data.Rows)
            {
                Supplier s = new Supplier();
                s.Supplier_Id = Convert.ToInt32(item["Supplier_Id"]);
                s.Supplier_Name = item["Supplier_Name"].ToString();
                s.Supplier_Address = item["Supplier_Address"].ToString();
                s.Supplier_Linkman = item["Supplier_Linkman"].ToString();
                s.Supplier_Phone = item["Supplier_Phone"].ToString();
                s.Supplier_Remark = item["Supplier_Remark"].ToString();
                s.P_Name = item["P_Name"].ToString();
                s.P_Id = Convert.ToInt32(item["P_Id"]);
                list.Add(s);
            }
            ViewBag.Supplier_Id = list;
            List<WareHose> plist = new List<WareHose>();
            DataTable dataer = dbll.WareHoseShow();
            foreach (DataRow item in dataer.Rows)
            {
                WareHose e = new WareHose();
                e.WareHose_Id = Convert.ToInt32(item[0]);
                e.WareHose_Name = item[1].ToString();
                e.WareHose_Address = item[2].ToString();
                e.WareHose_People = item[3].ToString();
                e.WareHose_Phone = item[4].ToString();
                e.WareHose_Remark = item[5].ToString();
                e.Commodity_Url = item[6].ToString();
                plist.Add(e);
            }
            ViewBag.WareHose_Id = plist;
            return View(bll.Porshow());
        }
        /// <summary>
        /// 根据最终日期查询
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="End_Time"></param>
        /// <returns></returns>
        public ActionResult SelPorBuy(string StartTime, string End_Time)
        {
            List<Supplier> list1 = new List<Supplier>();
            DataTable data1 = pbll.SupplierAll();
            foreach (DataRow item in data1.Rows)
            {
                Supplier s = new Supplier();
                s.Supplier_Id = Convert.ToInt32(item["Supplier_Id"]);
                s.Supplier_Name = item["Supplier_Name"].ToString();
                s.Supplier_Address = item["Supplier_Address"].ToString();
                s.Supplier_Linkman = item["Supplier_Linkman"].ToString();
                s.Supplier_Phone = item["Supplier_Phone"].ToString();
                s.Supplier_Remark = item["Supplier_Remark"].ToString();
                s.P_Name = item["P_Name"].ToString();
                s.P_Id = Convert.ToInt32(item["P_Id"]);
                list1.Add(s);
            }
            ViewBag.Supplier_Id = list1;
            List<WareHose> plist = new List<WareHose>();
            DataTable dataer = dbll.WareHoseShow();
            foreach (DataRow item in dataer.Rows)
            {
                WareHose e = new WareHose();
                e.WareHose_Id = Convert.ToInt32(item[0]);
                e.WareHose_Name = item[1].ToString();
                e.WareHose_Address = item[2].ToString();
                e.WareHose_People = item[3].ToString();
                e.WareHose_Phone = item[4].ToString();
                e.WareHose_Remark = item[5].ToString();
                e.Commodity_Url = item[6].ToString();
                plist.Add(e);
            }
            ViewBag.WareHose_Id = plist;
            List<PorBuy> list = new List<PorBuy>();
            DataTable data = bll.SelPorBuy(StartTime, End_Time);
            foreach (DataRow item in data.Rows)
            {
                PorBuy e = new PorBuy();
                e.Pid = Convert.ToInt32(item["Pid"]);
                e.PorBuy_Date =Convert.ToDateTime(item["PorBuy_Date"]);
                e.PorBuy_Cid= item["PorBuy_Cid"].ToString(); ;
                e.PorBuy_SumMoney = Convert.ToInt32(item["PorBuy_SumMoney"]);
                e.PorBuy_LastTime = Convert.ToDateTime(item["PorBuy_LastTime"]);
                e.Supplier_Id = Convert.ToInt32(item["Supplier_Id"]);
                e.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);
                e.Supplier_Name = item["Supplier_Name"].ToString();
                e.WareHose_Name = item["WareHose_Name"].ToString();
                list.Add(e);
            }
            return View("Procurement_Show", list);
        }
        /// <summary>
        /// 显示商品数据信息
        /// </summary>
        /// <returns></returns>
        public ActionResult show()
        {
           
            CMP.ERP.BLL.CommodityBLL bll = new CommodityBLL();
            ViewBag.CommodityType_Id = new SelectList(bll.GetCommodityTypeAll(), "CommodityType_Id", "CommodityType_Name");
            return Json(bll.GetCommodityAll(), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 实现添加采购订单功能
        /// </summary>
        /// <param name="PorBuy_Date"></param>
        /// <param name="Supplier_Id"></param>
        /// <param name="WareHose_Id"></param>
        /// <param name="PorBuy_Cid"></param>
        /// <returns></returns>
        /// <summary>
        /// 采购订单
        /// </summary>
        /// <param name="PorBuy_Date"></param>
        /// <param name="Supplier_Id"></param>
        /// <param name="WareHose_Id"></param>
        /// <param name="PorBuy_Cid"></param>
        /// <returns></returns>
        public ActionResult cer(int Supplier_Id, int WareHose_Id, string PorBuy_Cid)
        {
           

            PorBuy model = new PorBuy();
            model.PorBuy_Cid = PorBuy_Cid;
            model.Supplier_Id = Supplier_Id;
            model.WareHose_Id = WareHose_Id;
            string[] pronum = Request["Por_num"].ToString().Split(',');
            string[] commodityid = Request["Commodity_Id"].ToString().Split(',');
            string[] Commodity_price = Request["Commodity_price"].ToString().Split(',');
            List<PorBuyMate> list = new List<PorBuyMate>();
            for (int i = 0; i < pronum.Length; i++)
            {
                PorBuyMate s = new PorBuyMate();
                
                s.Commodity_Id = Convert.ToInt32(commodityid[i]);
                s.Por_num = Convert.ToInt32(pronum[i]);
                s.Commodity_Price = Convert.ToInt32(Commodity_price[i]);
                list.Add(s);
            }
            List<int> listnow = bll.Add(model,list).ToList();
            int content = 0;
            foreach (int item in listnow)
            {
                if (item == 1)
                {
                    content = 1;
                }
                else
                {
                    content = 0;
                    continue;
                }
            }
            if (content == 1)
            {
                return Content("True");
            }
            else
            {
                return Content("Fales");
            }
        }
        /// <summary>
        /// 获取供应商数据信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Experss_Show()
        {
            SupplierBLL blle = new SupplierBLL();
            ProvinceBLL pblle = new ProvinceBLL();
            //获取下拉框信息
            ViewBag.P_Id = new SelectList(pblle.GetProvince2(), "P_Id", "P_Name");
            //获取客户管理信息
            List<Supplier> list = new List<Supplier>();
            DataTable data = blle.SupplierAll();
            foreach (DataRow item in data.Rows)
            {
                Supplier s = new Supplier();
                s.Supplier_Id = Convert.ToInt32(item["Supplier_Id"]);
                s.Supplier_Name = item["Supplier_Name"].ToString();
                s.Supplier_Address = item["Supplier_Address"].ToString();
                s.Supplier_Linkman = item["Supplier_Linkman"].ToString();
                s.Supplier_Phone = item["Supplier_Phone"].ToString();
                s.Supplier_Remark = item["Supplier_Remark"].ToString();
                s.P_Name = item["P_Name"].ToString();
                s.P_Id = Convert.ToInt32(item["P_Id"]);
                list.Add(s);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 显示仓库信息
        /// </summary>
        /// <returns></returns>
        public ActionResult calendar_Show()
        {
            WareHoseBLL bllc = new WareHoseBLL();
            List<WareHose> list = new List<WareHose>();
            DataTable data = bllc.WareHoseShow();
            foreach (DataRow item in data.Rows)
            {
                WareHose e = new WareHose();
                e.WareHose_Id = Convert.ToInt32(item[0]);
                e.WareHose_Name = item[1].ToString();
                e.WareHose_Address = item[2].ToString();
                e.WareHose_People = item[3].ToString();
                e.WareHose_Phone = item[4].ToString();
                e.WareHose_Remark = item[5].ToString();
                e.Commodity_Url = item[6].ToString();
                list.Add(e);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }

          //删除采购订单
        public ActionResult DEL(int Pid)
        {
            List<int> listnow = bll.del(Pid).ToList();
             int content = 0;
            foreach (int item in listnow)
            {
                if (item == 1)
                {
                    content = 1;
                }
                else
                {
                    content = 0;
                    continue;
                }
            }
            if (content == 1)
            {
                return Content("True");
            }
            else
            {
                return Content("Fales");
            }
        }


        public ActionResult Search(int num)
        {
           
            CommodityBLL bcl=new CommodityBLL();
            //ViewBag.CommodityType_Id = new SelectList(bcl.GetCommodityTypeAll(), "CommodityType_Id", "CommodityType_Name");
            List<Commodity> list = new List<Commodity>();
            DataTable data = bcl.Search(num);
            foreach (DataRow item in data.Rows)
            {
                Commodity e = new Commodity();
                e.Commodity_Id = Convert.ToInt32(item["Commodity_Id"]);
                e.Commodity_Name = item["Commodity_Name"].ToString();
                e.Commodity_number = Convert.ToInt32(item["Commodity_number"]);
                e.Commodity_Remark = item["Commodity_Remark"].ToString();
                e.Commodity_Weight = Convert.ToInt32(item["Commodity_Weight"]);
                e.CommodityType_Id = Convert.ToInt32(item["CommodityType_Id"]);
                e.CommodityType_Name = item["CommodityType_Name"].ToString();
                e.Commodity_Price = Convert.ToInt32(item["Commodity_Price"]);
                list.Add(e);
            }
            return Json(list,JsonRequestBehavior.AllowGet);
        }
        public ActionResult SearchByName(string name)
        {
            name = Request["name"];
            CommodityBLL bcl = new CommodityBLL();
            return Json(bcl.SearchByName(name), JsonRequestBehavior.AllowGet);
        }
        
        public ActionResult Xiangqing(int id)
        {
            Session["id"] = Convert.ToInt32(id);
            List<Caigouxiangqing> list = new List<Caigouxiangqing>();
           
            DataTable data2 = bll.ShowXiangqing2(id);
            foreach (DataRow item in data2.Rows)
            {
                Caigouxiangqing c2 = new Caigouxiangqing();

                c2.Commodity_Id = Convert.ToInt32(item["Commodity_Id"]);
                Session["Commodity_Id"] = c2.Commodity_Id;
                c2.PorMaid = Convert.ToInt32(item["PorMaid"]);
                c2.Commodity_Name = item["Commodity_Name"].ToString();
                Session["Commodity_Name"] = c2.Commodity_Name;
                c2.Commodity_number = Convert.ToInt32(item["Commodity_number"]);
                c2.Commodity_Weight = Convert.ToInt32(item["Commodity_Weight"]);
                c2.Commodity_Remark = item["Commodity_Remark"].ToString();
                c2.Commodity_Price = Convert.ToInt32(item["Commodity_Price"]);
                c2.CommodityType_Name = item["CommodityType_Name"].ToString();
                c2.Por_num = Convert.ToInt32(item["Por_num"]);
                c2.PorMaid = Convert.ToInt32(item["PorMaid"]);
                Session["PorMaid"] = c2.PorMaid;
                list.Add(c2);
            }
            return View(list);
        }

        /// <summary>
        /// 主表显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult XQ(int id)
        {
            List<PorBuy> list = new List<PorBuy>();
            DataTable data2 = bll.ShowXiangqing1(id);
            foreach (DataRow item in data2.Rows)
            {
                PorBuy c2 = new PorBuy();
                c2.Pid = Convert.ToInt32(item["Pid"]);
                c2.PorBuy_Cid = item["PorBuy_Cid"].ToString() ;
                c2.PorBuy_Date = Convert.ToDateTime(item["PorBuy_Date"]);
                c2.PorBuy_LastTime = Convert.ToDateTime(item["PorBuy_LastTime"]);
                c2.PorBuy_SumMoney = Convert.ToInt32(item["PorBuy_SumMoney"]);
                c2.Supplier_Name = item["Supplier_Name"].ToString();
                c2.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);
                c2.WareHose_Name = item["WareHose_Name"].ToString();
                c2.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);

                list.Add(c2);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public ActionResult IntoStock(int Commodity_Id, int WareHose_Id, string Commodity_Name, string WareHose_Name, int PorMaid)
        {
            if (bll.IntoStock(Commodity_Id, WareHose_Id, Commodity_Name, WareHose_Name, PorMaid) > 0)
            {
                return Content("True");
            }
            else
            {
                return Content("Fales");
            }
        }
        public ActionResult update(string pidtr)
        {
            string[] pidtre = pidtr.ToString().Split(',');
            List<PorBuyMate> list = new List<PorBuyMate>();
            int Pid=0;
            for (int i = 0; i < pidtre.Length; i++)
            {     
                PorBuyMate s = new PorBuyMate();
                foreach (var item in bll.huoquiry(Convert.ToInt32(pidtre[i])))
                {
                    s.Commodity_Id = item.Commodity_Id;
                    Pid = item.Pid;
                    s.Por_num = item.Por_num;
                } 
          
                list.Add(s);
            }
            List<int> listnow = bll.Update(Pid, list).ToList();
            int content = 0;
            foreach (int item in listnow)
            {
                if (item == 1)
                {
                    content = 1;
                }
                else
                {
                    content = 0;
                    continue;
                }
            }
            if (content == 1)
            {
                return Content("True");
            }
            else
            {
                return Content("Fales");
            }
            

        }
    }
}

